#!/usr/bin/python
import sys
sys.dont_write_bytecode = True
import unittest
import libs.sqliteConn as sqliteConn
import datetime
import os

def read_and_split_file(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
            items = content.split(';')
            return items
    except FileNotFoundError:
        print("错误: 文件未找到!")
    except Exception as e:
        print(f"错误: 发生了一个未知错误: {e}")

class TestSQLite(unittest.TestCase):
  def setUp(self):
    # self.path = '/tmp/test_db.db'
    self.path = ":memory:"
    self.db = sqliteConn.Connection(self.path)
    
  def tearDown(self):
    self.db.close()
    # os.unlink(self.path)
 
    
  def testCreateInsert(self):
 
    file_path = '/data/sqlite/init.sql'
    items = read_and_split_file(file_path)
    if items: # 如果是 NoneType 没有读取到文件。
      for item in items:
        print(item.strip())
        self.db.execute(item.strip())

    #result = self.db.query("SELECT * FROM stocks")
    #self.assertNotEqual(result, [])
    
if __name__ == '__main__':
  print("############## start ##############")
  unittest.main()